<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item><i class="el-icon-date"></i> 个人中心</el-breadcrumb-item>
        <el-breadcrumb-item>修改密码</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="form-box">
        <el-form :model="form" :rules="rules" ref="form" label-width="80px">
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input v-model="form.oldPassword" type="password" class="password" ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="password">
            <el-input v-model="form.password" type="password" class="password" ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirm_password">
            <el-input v-model="form.confirm_password" type="password" class="password" ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit('form')">确认修改</el-button>

          </el-form-item>
        </el-form>
      </div>
    </div>

  </div>
</template>

<script>
  import bus from '../../common/bus';
  import md5 from 'js-md5';
  export default {
    data: function(){
      /**
       * 验证旧密码是否正确
       * @param rule
       * @param value
       * @param callback
       */
      var validateOldPass = (rule, value, callback) => {
          let password = JSON.parse(sessionStorage.getItem('userMap')).password;
          if(md5(value) == password){
            callback();
          }else{
            callback(new Error('旧密码不正确'));
          }
      };
      /**
       * 验证密码位数
       * @param rule
       * @param value
       * @param callback
       */
      var validatePass = (rule, value, callback) => {
        if (!/^[a-zA-Z0-9]{6,20}$/.test(value)) {
          callback(new Error('密码必须6-20位数字'));
        } else {
          callback();
        }
      };


      /**
       * 验证密码是否一致
       * @param rule
       * @param value
       * @param callback
       */
      var validatePass2 = (rule, value, callback) => {
        if (value != this.form.password) {
          callback(new Error('两次密码输入不一致'));
        } else {
          callback();
        }
      };
      return {
        form: {
          oldPassword: '',
          password: '',
          confirm_password: ''
        },
        rules: {
          oldPassword: [
            { required: true, message: '请输入旧密码', trigger: 'blur' },
            { validator: validateOldPass, trigger: 'blur' },
            { validator: validatePass, trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入新密码', trigger: 'blur' },
            { validator: validatePass, trigger: 'blur' }
          ],
          confirm_password: [
            { required: true, message: '请输入确认密码', trigger: 'blur' },
            { validator: validatePass, trigger: 'blur' },
            { validator: validatePass2, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {

      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.updatePassword();

          }

        });


      },
      updatePassword(){
        var that = this;
        this.HttpUtil.encryptPost(
          this,
          this.Global.URLPATH.MANAGE_UPDATE_PASSWORD,
          {
            oldPassword :md5(that.form.oldPassword),
            password : md5(that.form.password),
            token:sessionStorage.getItem("token")
          },
          function (data) {
            if(data.status == 0){
              that.$message.success('密码修改成功！');
              let userMap = JSON.parse(sessionStorage.getItem('userMap'))
              userMap.password = md5(that.form.password)
              sessionStorage.setItem("userMap",JSON.stringify(userMap))
              that.form.oldPassword = '';
              that.form.password ='';
              that.form.confirm_password ='';
            }
          },
          function (data) {//访问失败
            that.$message.success(data.msg);
          });
      }

    }
  }
</script>
<style>
	@import url("../../../assets/css/common.css");
  .password{
    width: 250px;
  }
</style>
